package com.ruoyi.web.controller.business.statistics;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.goods.domain.vo.JbProductVO;
import com.ruoyi.business.goods.service.JbCommodityService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.controller.business.goods.factory.JbCommodityFactory;
import com.ruoyi.web.controller.business.rule.factory.JbRulesFactory;
import com.ruoyi.web.controller.business.statistics.factory.JbCommissionFactory;
import com.ruoyi.web.controller.business.statistics.factory.JbGrossProfitRateFactory;
import com.ruoyi.web.controller.business.statistics.vo.JbFindInventoryVO;
import com.ruoyi.web.controller.business.statistics.vo.JbGrossProfitRateQueryVO;
import com.ruoyi.web.controller.business.statistics.vo.JbQuickQueryVO;
import com.ruoyi.web.controller.system.factory.SysDataScopeFactory;
import com.ruoyi.web.controller.thirdparty.factory.ThirdpartyChanjetFactory;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 业务工具 控制器
 *
 * @author Jyl
 */
@RestController
@RequestMapping("/jb_tool")
public class JbToolController extends JoController
{

    @Autowired
    private JbGrossProfitRateFactory jbGrossProfitRateFactory;
    @Autowired
    private JbCommissionFactory jbCommissionFactory;
    @Autowired
    private JbCommodityService jbCommodityService;
    @Autowired
    private JbCommodityFactory jbCommodityFactory;
    @Autowired
    private ThirdpartyChanjetFactory thirdpartyChanjetFactory;
    @Autowired
    private JbRulesFactory jbRulesFactory;
    @Autowired
    private SysDataScopeFactory sysDataScopeFactory;

    /**
     * 计算 毛利率
     *
     * @param queryVO
     */
    @PostMapping("fnGrossProfitRate")
    public AjaxResult fnGrossProfitRate(@RequestBody JbGrossProfitRateQueryVO queryVO)
    {
        return JoR.success(jbGrossProfitRateFactory.fnGrossProfitRate(queryVO.getList(), queryVO.getLogisticsId(),
                queryVO.getAreaCode(), queryVO.getOrderTotalAmount()));
    }

    /**
     * 计算 订单提成
     */
    @PostMapping(value = "fnRoyalties")
    public AjaxResult fnRoyalties(@Valid @RequestBody JbGrossProfitRateQueryVO queryVO)
    {
        return success(jbCommissionFactory.fnRoyalties(queryVO));
    }

    /**
     * 查询 快速查询 商品join产品
     *
     * @param pageParam
     * @param jbProductVO
     */
    @GetMapping("quickQuery")
    public TableDataInfo quickQuery(PageParam pageParam, JbProductVO jbProductVO)
    {
        String channel = getUser().getDept().getChannel();
        JSONObject commodity = new JSONObject()
                .fluentPut("channel", channel)
                .fluentPut("ids", sysDataScopeFactory.fnStrDataScope("1"));

        IPage<JbProductVO> page = jbCommodityService.pageCommodityProduct(pageParam, jbProductVO, commodity);
        List<JbQuickQueryVO> reList = BeanUtil.copyToList(page.getRecords(), JbQuickQueryVO.class);
        return getDataTable(reList, page.getTotal());
    }

    /**
     * 查询 库存查询 商品join产品join库存
     *
     * @param pageParam
     * @param jbProductVO
     */
    @GetMapping("findInventory")
    public TableDataInfo findInventory(PageParam pageParam, JbProductVO jbProductVO)
    {
        IPage<JbProductVO> page = jbCommodityService.findInventory(pageParam, jbProductVO);
        List<JbFindInventoryVO> reList = BeanUtil.copyToList(page.getRecords(), JbFindInventoryVO.class);
        return getDataTable(reList, page.getTotal());
    }

    /**
     * 更新库存 第三方用友商品库存
     */
    @GetMapping("refreshInventory")
    public AjaxResult refreshInventory()
    {
        return AjaxResult.success(thirdpartyChanjetFactory.refreshInventory());
    }

}
